package lv.accenture.ex05;

public class Complex {
	private final float re;

	private final float im;

	private Complex(float re, float im) {
		this.re = re;
		this.im = im;
	}

	public static Complex valueOf(float re, float im) {
		return new Complex(re, im);
	}

	// Accessors with no corresponding mutators
	public float getRe() {
		return re;
	}

	public float getIm() {
		return im;
	}

	public Complex add(Complex c) {
		return new Complex(re + c.re, im + c.im);
	}

	public Complex subtract(Complex c) {
		return new Complex(re - c.re, im - c.im);
	}

	public Complex multiply(Complex c) {
		return new Complex(re * c.re - im * c.im, re * c.im + im * c.re);
	}

	public Complex divide(Complex c) {
		float tmp = c.re * c.re + c.im * c.im;
		return new Complex((re * c.re + im * c.im) / tmp, (im * c.re - re
				* c.im)
				/ tmp);
	}

	public boolean equals(Object o) {
		if (o == this)
			return true;
		if (!(o instanceof Complex))
			return false;
		Complex c = (Complex) o;
		return (Float.floatToIntBits(re) == Float.floatToIntBits(c.re))
				&& (Float.floatToIntBits(im) == Float.floatToIntBits(im));
	}

	public int hashCode() {
		int result = 17 + Float.floatToIntBits(re);
		result = 37 * result + Float.floatToIntBits(im);
		return result;
	}

	public String toString() {
		return "(" + re + " + " + im + "i)";
	}
}
